import logging
import sys
import traceback

from student_service import StudentService
from survey_service import SurveyService


class ServiceHandler(object):
  @staticmethod
  def getService(service_name, user):
    if service_name == 'studentservice':
      return StudentService(user)
    if service_name == 'surveyservice':
      return SurveyService(user)

  @staticmethod
  def handleRequest(user, service_name, method_name, request):
    service = ServiceHandler.getService(service_name, user)
    try:
      return getattr(service, method_name)(request)
    except:
      # Log traceback and return the error.
      logging.info(traceback.format_exc())
      return {
        'error': {
          'external_message': 'Could not complete your request.',
          'internal_message': traceback.format_exc()
        }
      }
